<?php
/**
 * 提交数据验证<br />
 * 对提交的 get|post|request 等数据进行验证
 * @author 41412754@qq.com
 */
class validate {
	
	private $field;
	private $form;
	private $name;
	private $value;
	private $msg;
	
	public $msgs;
	
	public function __construct()
	{
		$this->app = $GLOBALS['app'];
	}
	
	/**
	 * 接受验证
	 * @param $field
	 * @param $name 可选
	 */
	public function check( $field, $name = null )
	{
		$fields = explode( '.', $field );
		$form = end( $fields );
		$this->name = $name ? $name : $form;
		
		$type = array_shift( $fields );
		if ( $type == 'post' ) $var = '$_POST';
		elseif ( $type == 'get' ) $var = '$_GET';
		elseif ( $type == 'request' ) $var = '$_REQUEST';
		else trigger_error( '不能接受的参数' );
		
		$this->field = $var . '[\'' . implode( '\'][\'', $fields ) . '\']';
		$this->value = eval( 'return ' . $this->field . ';' );
		
		$this->isEmpty = trim( $this->value ) == '';
		$this->isNotEmpty = ! $this->isEmpty;
		
		return $this;
	}
	
	public function get( $field, $name = null )
	{
		return self::check( 'get.' . $field, $name );
	}
	
	public function post( $field, $name = null )
	{
		return self::check( 'post.' . $field, $name );
	}
	
	public function request( $field, $name = null )
	{
		return self::check( 'request.' . $field, $name );
	}
	
	/**
	 * 设置默认值
	 * @param $value
	 */
	public function defaults( $value )
	{
		if ( $this->isEmpty )
		{
			eval( $this->field . '=$value;' );
			$this->value = $value;
		}
		
		return $this;
	}
	
	/**
	 * 结束验证并取得当前验证结果
	 * @param $exitIfErr
	 */
	public function end( $exitIfErr = false )
	{
		if ( $this->msg )
		{
			$msg = sprintf( $this->msg, $this->name );
			$this->msgs[] = $msg;
			$this->msg = null;
			if ( $exitIfErr ) exit( $msg );
		}
		return $this->value;
	}
	
	/**************** 验证函数 ************************************/
	
	/**
	 * 必选字段
	 * @param $msg
	 */
	public function required( $msg = '%s 不能为空' )
	{
		if ( ! $this->msg && $this->isEmpty ) $this->msg = $msg;
		return $this;
	}
	
	/**
	 * 数字字段
	 * @param $msg
	 */
	public function number( $msg = '%s 必需是数字' )
	{
		if ( ! $this->msg && $this->isNotEmpty )
		{
			if ( ! is_numeric( $this->value ) )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
	
	/**
	 * 日期字段
	 * @param $msg
	 */
	public function dateISO( $msg = '%s 必须是Y-m-d日期格式' )
	{
		if ( ! $this->msg and $this->isNotEmpty )
		{
			if ( ! preg_match( '/^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/i', $this->value ) )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
	
	/**
	 * 邮件字段
	 * @param $msg
	 */
	public function email( $msg = '%s 必须是email' )
	{
		if ( ! $this->msg and $this->isNotEmpty )
		{
			if ( ! preg_match( "/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i", $this->value ) )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
	
	/**
	 * 字段最小值
	 * @param $min
	 * @param $msg
	 */
	public function min( $min, $msg = '%s 应大于 %u' )
	{
		$msg = sprintf( $msg, '%s', $min );
		if ( ! $this->msg and $this->isNotEmpty )
		{
			if ( $this->value < $min )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
	
	/**
	 * 字段最大值
	 * @param $max
	 * @param $msg
	 */
	public function max( $max, $msg = '%s 应小于 %u' )
	{
		$msg = sprintf( $msg, '%s', $max );
		if ( ! $this->msg and $this->isNotEmpty )
		{
			if ( $this->value > $max )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
	
	/**
	 * 字段最小长度
	 * @param $minlength
	 * @param $msg
	 */
	public function minlength( $minlength, $msg = '%s 至少要有 %u 位' )
	{
		$msg = sprintf( $msg, '%s', $minlength );
		if ( ! $this->msg and $this->isNotEmpty )
		{
			if ( strlen( $this->value ) < $minlength )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
	
	/**
	 * 字段最大长度
	 * @param $maxlength
	 * @param $msg
	 */
	public function maxlength( $maxlength, $msg = '%s 最多只有 %u 位' )
	{
		$msg = sprintf( $msg, '%s', $maxlength );
		if ( ! $this->msg and $this->isNotEmpty )
		{
			if ( strlen( $this->value ) > $maxlength )
			{
				$this->msg = $msg;
			}
		}
		return $this;
	}
}
